package com.prs.crm.service.crm.impl;

import java.io.File;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;

import com.prs.crm.context.FileUtil;
import com.prs.crm.context.ToolKits;
import com.prs.crm.dao.crm.LiaisonDao;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.domain.crm.Liaison;
import com.prs.crm.domain.crm.LiaisonCost;
import com.prs.crm.service.crm.LiaisonCostItemService;
import com.prs.crm.service.crm.LiaisonCostService;
import com.prs.crm.service.crm.LiaisonService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class LiaisonServiceImpl extends BaseServiceImpl<Liaison,LiaisonDao> implements LiaisonService{
	private LiaisonCostItemService liaisonCostItemService;
	private LiaisonCostService liaisonCostService;
	private Date startTime;
	private Date endTime;
	private Customer customer;
	@Override
	public void saveLiaison(Liaison liaison, File file, String fileName,
			Integer[] items, Double[] num) {
		// TODO Auto-generated method stub
		liaison.setAttach(FileUtil.getInstance().createAttach(file, fileName));
		
		int i=0;

		this.getDao().save(liaison);
		for (Integer itemid : items)
		{
			LiaisonCost cost = new LiaisonCost();
			cost.setLiaison(liaison);
			cost.setItem(liaisonCostItemService.get(itemid));
			if(num != null) {
				cost.setValue(num[i++]);
			}else {
				cost.setValue(0.00);
			}
			
			liaisonCostService.save(cost);
		}
	}

	public void setLiaisonCostItemService(LiaisonCostItemService liaisonCostItemService) {
		this.liaisonCostItemService = liaisonCostItemService;
	}

	public LiaisonCostItemService getLiaisonCostItemService() {
		return liaisonCostItemService;
	}

	public void setLiaisonCostService(LiaisonCostService liaisonCostService) {
		this.liaisonCostService = liaisonCostService;
	}

	public LiaisonCostService getLiaisonCostService() {
		return liaisonCostService;
	}

	@Override
	public Collection<Liaison> getAllLiaison() {
		// TODO Auto-generated method stub
		return this.getDao().getAllLiaison();
	}

	@Override
	public Collection<Liaison> getTodayDueLiaison() {
		// TODO Auto-generated method stub
		this.getDao().isExistLiaison(startTime, endTime, customer);
		Collection<Liaison> results = new ArrayList<Liaison>();
		Collection<Liaison> liaisons = this.getDao().getAllLiaison();
		Calendar todayCalendar = Calendar.getInstance();
		for(Liaison liaison : liaisons) {
			if(liaison.getFlag() != null) {
				
				continue;
			}
			Calendar liaisonCalendar = Calendar.getInstance();
			if(liaison.getNextLiaison() != null) {
				liaisonCalendar.setTime(liaison.getNextLiaison());
				
				if((todayCalendar.get(Calendar.YEAR) == liaisonCalendar.get(Calendar.YEAR)) && (todayCalendar.get(Calendar.MONTH) == liaisonCalendar.get(Calendar.MONTH)) && (todayCalendar.get(Calendar.DAY_OF_MONTH) == liaisonCalendar.get(Calendar.DAY_OF_MONTH))) {
					results.add(liaison);
				}
			}
		}
		return results;
	}

	@Override
	public Collection<Liaison> getLiaison(Customer customer) {
		// TODO Auto-generated method stub
		return this.getDao().getLiaison(customer);
	}

	@Override
	public Collection<Liaison> getLiaisonByIds(Integer[] ids) {
		// TODO Auto-generated method stub
		return this.getDao().getLiaisonByIds(ToolKits.convertToIntAry(ids));
	}

}
